package com.hjs.test.test;

import java.util.Arrays;
import java.util.Stack;
import java.util.stream.Stream;

public class T {
    public static void main(String[] args) {

        T t = new T();
        //t.test();

        int[] temperatures = {73, 74, 75, 71, 69, 72, 76, 73};
        int[] answer = t.dailyTemperatures(temperatures);
        System.out.println(Arrays.toString(temperatures));
        System.out.println(Arrays.toString(answer));
    }

    public void test(){
        try{
            System.out.println("before exception ...");
            int i = 1/0;
            System.out.println("after exception ...");
        } catch (Exception e){
            System.out.println("catch ...");
        } finally {
            System.out.println("finally ...");
        }
    }

    public int[] dailyTemperatures(int[] temperatures) {
        int[] answer = new int[temperatures.length];
        Stack<Integer> stack = new Stack<>();

        for (int i = 0; i < temperatures.length; i++) {
            while (!stack.isEmpty() && temperatures[i] > temperatures[stack.peek()]) {
                int index = stack.pop();
                answer[index] = i - index;
            }
            stack.push(i);
        }

        // 处理栈中剩余的元素，这些元素对应的答案都是0，因为后面没有更高的温度了
        while (!stack.isEmpty()) {
            answer[stack.pop()] = 0;
        }

        return answer;
    }

}
